﻿/*
 * Copyright © 2011 Tristan Reeves, treeves@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Sprinkles.Secondary;

namespace Sprinkles.ExpressionHandlers
{
    internal class MethodExpressionHandler<T>
    {
        private List<Type> m_ArgumentTypes = new List<Type>();
        private readonly List<Arg> m_Arguments = new List<Arg>();
        private bool ProcessInternal(MethodCallExpression expression)
        {
            if (expression == null) return false;
            var target = expression.Object;
            var mi = expression.Method;
            var mi_args = mi.GetParameters();
            var args = expression.Arguments;

            this.IsStatic = mi.IsStatic;
            if (!mi.IsStatic)
            {
                var p = target as ParameterExpression;
                if (p == null) return false;
                if (p.Type != typeof(T)) return false;
            }
            if (mi.IsSpecialName) return false;
            if (args.Count != m_ArgumentTypes.Count) return false;
            if (mi_args.Length != m_ArgumentTypes.Count) return false;
            for (int i = 0; i < args.Count; i++)
            {
                var pe_arg = args[i] as ParameterExpression;
                if (pe_arg == null) return false;
                if (pe_arg.Type != mi_args[i].ParameterType) return false;
                if (pe_arg.Type != m_ArgumentTypes[i]) return false;
            }

            this.IsStatic = mi.IsStatic;
            this.MethodName = mi.Name;
            this.ReturnType = mi.ReturnType;
            this.DeclaringType = mi.DeclaringType;
            this.IsVirtual = mi.IsVirtual || mi.IsAbstract;

            for (int i = 0; i < mi_args.Length; i++)
            {
                var a = mi_args[i];
                var arg = new Arg() { DeclaredType = a.ParameterType, Index = i, Name = a.Name, Value = null };
                m_Arguments.Add(arg);
            }
            return true;
        }
        public void Process(LambdaExpression expression, params Type[] argumentTypes)
        {
            IsValid = false;
            m_ArgumentTypes = (argumentTypes ?? new Type[0]).ToList();

            var mce = expression.Body as MethodCallExpression;
            if (mce == null) return;
            IsValid = ProcessInternal(mce);
        }

        public Type DeclaringType { get; private set; }
        public Type ReturnType { get; private set; }
        public bool IsValid { get; private set; }
        public bool IsVirtual { get; private set; }
        public string MethodName { get; private set; }
        public bool IsStatic { get; private set; }
        public Arg[] Arguments { get { return m_Arguments.ToArray(); } }
    }
}
